/*
 * @Description: 按键模块-bsp层
 * @Author: yangsw
 * @Date: 2021-06-11 09:49:42
 * @LastEditTime: 2022-06-17 10:43:05
 * @LastEditors: yangsw
 * @Reference: 
 */
#include "bsp_key.h"
#ifdef ZCODE
#include "../../log/svc/svc_log.h"
#endif

/**
 * @brief: 按键GPIO初始化
 * @param  {void}
 * @return {void}
 * @note: 
 * @see: 
 */
void Bsp_Key_Gpio_Init(void){
    
    GPIO_InitType GPIO_InitStructure;
    EXTI_InitType EXTI_InitStructure;
    NVIC_InitType NVIC_InitStructure;
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
    GPIO_InitStruct(&GPIO_InitStructure);
    GPIO_InitStructure.Pin        = KEY_PIN;
    GPIO_InitStructure.GPIO_Pull  = GPIO_No_Pull;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Input;
    GPIO_InitPeripheral(KEY_PORT, &GPIO_InitStructure);

    /*Configure key EXTI Line to key input Pin*/
    GPIO_ConfigEXTILine(KEY_INPUT_PORT_SOURCE, KEY_INPUT_PIN_SOURCE);

    /*Configure key EXTI line*/
    EXTI_InitStructure.EXTI_Line    = KEY_INPUT_EXTI_LINE;
    EXTI_InitStructure.EXTI_Mode    = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; // EXTI_Trigger_Rising;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_InitPeripheral(&EXTI_InitStructure);
    /*Set key input interrupt priority*/
    NVIC_InitStructure.NVIC_IRQChannel                   = KEY_INPUT_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0x03;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_Init(&NVIC_InitStructure);                                                                   //开中断
}

/**
 * @brief: 获取按键当前状态
 * @param  {void}
 * @return {E_Gpio_State} 按键当前状态
 * @note: 
 * @see: 
 */
E_Gpio_State Bsp_Key_Get_State(void){
    if(GPIO_ReadInputDataBit(KEY_PORT,KEY_PIN)){
        return E_Gpio_Set;
    }else{
        return E_Gpio_Reset;
    }   
}